package com.hsc.yalebao.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.hsc.yalebao.model.ChatMsgEntity;
import com.hsc.yalebao.weight.LogUtils;
import com.j256.ormlite.android.DatabaseTableConfigUtil;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.DatabaseTableConfig;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChatMessageUtil
{
  private static ChatMessageUtil instance;
  private String TAG = "ChatMessageUtil";
  private Map<String, Dao<ChatMsgEntity, Integer>> mDaoMap = new HashMap();
  private DatabaseHelper mHelper;

  public ChatMessageUtil(Context paramContext)
  {
    this.mHelper = ((DatabaseHelper)OpenHelperManager.getHelper(paramContext, DatabaseHelper.class));
  }

  private void createTableIfNotExist(String paramString)
  {
    if (isTableExist(paramString))
      return;
    String str = "CREATE TABLE " + paramString + " ( sendtime VARCHAR , " + " nickname VARCHAR , body VARCHAR , picurl VARCHAR , " + " messagetype VARCHAR , user_name VARCHAR , isComMsg INTEGER, isSendMsg INTEGER , memberid INTEGER)";
    this.mHelper.getWritableDatabase().execSQL(str);
    LogUtils.e(this.TAG + "roamer", "isTableExist(tableName):" + isTableExist(paramString));
  }

  private Dao<ChatMsgEntity, Integer> getDao(String paramString)
  {
    if (this.mDaoMap.containsKey(paramString))
      return (Dao)this.mDaoMap.get(paramString);
    try
    {
      DatabaseTableConfig localDatabaseTableConfig = DatabaseTableConfigUtil.fromClass(this.mHelper.getConnectionSource(), ChatMsgEntity.class);
      localDatabaseTableConfig.setTableName(paramString);
      createTableIfNotExist(paramString);
      Dao localDao2 = UnlimitDaoManager.createDao(this.mHelper.getConnectionSource(), localDatabaseTableConfig);
      localDao1 = localDao2;
      if (localDao1 != null)
        this.mDaoMap.put(paramString, localDao1);
      return localDao1;
    }
    catch (SQLException localSQLException)
    {
      while (true)
      {
        localSQLException.printStackTrace();
        Dao localDao1 = null;
      }
    }
  }

  public static ChatMessageUtil getInstance(Context paramContext)
  {
    if (instance == null)
      instance = new ChatMessageUtil(paramContext);
    return instance;
  }

  private boolean isTableExist(String paramString)
  {
    if (paramString == null)
    {
      LogUtils.e(this.TAG + "isTableExist", "tableName == null");
      return false;
    }
    Cursor localCursor = null;
    try
    {
      String str = "select count(*) asc from Sqlite_master  where type ='table' and name ='" + paramString.trim() + "' ";
      localCursor = this.mHelper.getReadableDatabase().rawQuery(str, null);
      boolean bool2 = localCursor.moveToNext();
      bool1 = false;
      if (bool2)
      {
        int i = localCursor.getInt(0);
        bool1 = false;
        if (i > 0)
          bool1 = true;
      }
      return bool1;
    }
    catch (Exception localException)
    {
      while (true)
      {
        localException.printStackTrace();
        boolean bool1 = false;
        if (localCursor != null)
        {
          localCursor.close();
          bool1 = false;
        }
      }
    }
    finally
    {
      if (localCursor != null)
        localCursor.close();
    }
  }

  public void addMessage(String paramString, ChatMsgEntity paramChatMsgEntity)
  {
    Dao localDao = getDao(paramString);
    try
    {
      int i = localDao.create(paramChatMsgEntity);
      LogUtils.d(this.TAG + "addMessage", "succeed:" + i);
      return;
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
  }

  public void deleteMessage(String paramString, ChatMsgEntity paramChatMsgEntity)
  {
    Dao localDao = getDao(paramString);
    try
    {
      int i = localDao.delete(paramChatMsgEntity);
      LogUtils.d(this.TAG + "addMessage", "succeed:" + i);
      return;
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
  }

  public List<ChatMsgEntity> getAllMessage(String paramString)
  {
    try
    {
      List localList = getDao(paramString).queryForAll();
      return localList;
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
    return null;
  }

  public void updateMessage(String paramString, ChatMsgEntity paramChatMsgEntity)
  {
    Dao localDao = getDao(paramString);
    try
    {
      int i = localDao.update(paramChatMsgEntity);
      LogUtils.d(this.TAG + "addMessage", "succeed:" + i);
      return;
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.hsc.yalebao.db.ChatMessageUtil
 * JD-Core Version:    0.6.2
 */